<?php

namespace app\api\controller;

use app\common\business\User as userBus;

class User extends AuthBase
{
    public function index()
    {
        $userBus = new userBus();
        $user = $userBus->getNormalUserById($this->userId);
        $resultUser = [
            'id' => $user['id'],
            'username' => $user['username'],
            'sex' => $user['sex'],
        ];
        return show(config("status.success"), "OK", $resultUser);
    }

    public function update()
    {
        if (!($this->request->isPut())) {
            return show("status.error", "非法请求");
        }
        $username = input("username", "", "trim");
        $sex = input("sex", '', 'trim');
        $data = [
            'username' => $username,
            'sex' => $sex,
            'operate_user' => $username
        ];
        $validate = new \app\api\validate\User();
        if (!$validate->scene('update_user')->check($data)) {
            return show(config("status.error"), $validate->getError());
        }
        $userBus = new userBus();
        $user = $userBus->update($this->userId, $data, $this->accessToken);
        if (!$user) {
            return show(config("status.error"), "更新失败");
        }
        return show(config("status.success"), "更新成功");

    }
}